﻿var NDrag = {
    Data:{"0":null},
    State:0,
    DragState:0,
    ContainerPos:{},
    PosData:{},
    Draging:false,
    DragElement:0,
    TargetElement:null,
    TempElement:null,
    init:function(){
        this.ContainerPos=$('#privateEntries1').position();
        $("#dragItem").easydrag(true);
        $("#dragItem").ondrop(function(e,element){
            if(NDrag.ActionName){
                NDrag.cmd(NDrag.ActionName);
            }else{
                NDrag._drag(element);
            }
            setTimeout(function(){$('#search-box').show();},300);
            return false;
        }).mouseover(function(e){
                if($.browser.msie)window.status=NDrag.Data[NDrag.DragElement][2];
                return false;
        }).mouseout(function(e){
                NDrag.mouseOutBox();
                return false;
        }).ondrag(function(e){
                NDrag.Draging=true;
                NDrag._getTarget(e);
                return false;
        }).click(function(e){
                NDrag.clickBox(NDrag.DragElement);
                return false;
        });
        $("#privateEntries1 .cell").mouseover(function(e){
                NDrag.mouseOverBox(this);
                return false;
            });
        for(var i=0;i< $("#privateEntries1").children().length;i++)
        {
            var item = $("#privateEntries1").children()[i];
            
            NDrag.Data[i] = $($("#privateEntries1").children()[i]);
            var pos=$(item).position();
            NDrag.PosData[i]=[pos.left,pos.top,pos.left+80,pos.top+90];
        }
    },
    
    exchange:function(sourceId,targetId){
    
        NDrag.DragElement=NDrag.TargetElement;

        var sourceElement=NDrag.Data[sourceId];
        var targetElement=NDrag.Data[targetId];
        targetElement.removeClass('target');
        var _html=targetElement.html();
        targetElement.html(sourceElement.html());
        sourceElement.html(_html);
        
        //$('#test').html(targetElement.attr('id') + "-" + sourceElement.attr('id'));
        
        var _itemId = targetElement.attr('id');
        targetElement.attr('id',sourceElement.attr('id'));
        sourceElement.attr('id',_itemId);
        
        //document.getElementById('test').innerHTML += "-" + targetElement.attr('id') + "-" + sourceElement.attr('id');
        
        //var _data=NDrag.Data[sourceId];
        //NDrag.Data[sourceId]=NDrag.Data[targetId];
        //NDrag.Data[targetId]=_data;
        
        //var _pos = NDrag.PosData[targetId];
        //NDrag.PosData[targetId] = NDrag.PosData[sourceId];
       // NDrag.PosData[sourceId] = _pos;
        
        $("#dragItem").html('');
        $("#dragItem").hide();
        if(this.State!=2)return;
        var data=$R(0,51).map(function(id){
            return NDrag.Data[id]?NDrag.Data[id][0]:0;
        }).join(',');
        jQuery.post('?action=editDesk','id='+this.DefaultDesk+'&data='+data);
        },
        updateSite:function(id,data){
        data.id=data.id||0;
        this.updateSiteData(id,data);
        NDrag.initBox(id);
        var queryStr='deskId='+this.DefaultDesk+'&index='+id+'&data='+this.Data[id].join(',');
        jQuery.post('?action=updateDeskSite',queryStr,function(result){
        if(result){
        data.id=parseInt(result);
        if(!data.id)return false;
        NDrag.updateSiteData(id,data);
        }
        });
    },
    
    _drag:function(e){
        var id=NDrag.TargetElement;

        $('#dragItem').animate({
            left:NDrag.PosData[id][0],
            top:NDrag.PosData[id][1]
            },200,function(){
                NDrag.Draging=false;
                if(NDrag.TargetElement!=NDrag.DragElement){
                    NDrag.exchange(NDrag.DragElement,NDrag.TargetElement);
                }else{
                    $('#'+id).removeClass('draging');
                }
         NDrag._dragShowAction();
        });
    },
    mouseOverBox:function(e){
        if(this.Draging)return;
        if(!this.MouseOut)this.mouseOutBox();
        this.MouseOut=false;
        
        for(var i=0;i< $("#privateEntries1").children().length;i++)
        {
            var item = $("#privateEntries1").children()[i];
            if(item.id == e.id)
                var id=i;
         }
        if(!this.Data[id]){
        }else{
            this.DragElement=this.TempElement=this.TargetElement=id;
            var dragHTML=$(this.Data[id]).html().replace(/id=[^> ]*/img,'');
            $(this.Data[id]).addClass('draging');
            $("#dragItem").html(dragHTML).css({left:this.PosData[id][0],top:this.PosData[id][1]}).show();
        }
    },
    mouseOutBox:function(e){
        if(this.Draging)return;
        $("#dragItem").hide();
        if($.browser.msie)window.status='';
        $(NDrag.Data[this.DragElement]).removeClass('draging');
        this.MouseOut=true;
    },
    clickBox:function(id){
        if(this.Draging)return;
        var val = $(NDrag.Data[id].children()[0]).html().split('|');
        var url=val[0];
        window.open(url,'');
    },
    dragOverBox:function(id){
        //$('#'+this.TempElement).removeClass('dragOver');
        $('#'+id).addClass('dragOver');
    },
    
    _dragAction:function(action){
        if(action==this.ActionName)return;
        $('#'+this.TempElement).removeClass('dragOver');
        this.TempElement=null;
        this.ActionName=action;
        $('#'+action).addClass('hover');
        },
        cmd:function(action){
        var id=this.DragElement;
        if(this.State==0){
        $('#loginDialog').click();
        }else{
        this[action.split('-')[1]+'Site'](id);
        }
        this.ShowAction=false;
        this.ActionName=null;
        setTimeout(function(){
        $('#'+action).removeClass('hover');
        $("#dragItem").hide();
        $('#'+(NDrag.State==2?'opt-mine':'opt-common')).hide();
        NDrag.Draging=false;
        },action=='mine-del'?0:500);
    },
    _dragShowAction:function(){
    },
    _getTarget:function(e){
    
        var pos=$.getMousePosition(e);
        for(var k=0;k< $("#privateEntries1").children().length;k++)
        {
        
            var item1 = $("#privateEntries1").children()[k];
            var posItem=$(item1).position();
            
            
             if (pos.y <= posItem.top + 50 && pos.x <= posItem.left + 120 && pos.y >= posItem.top && pos.x >=posItem.left ) {
                NDrag.TargetElement =k;
                break;
            }
            //$(NDrag.Data[k]).removeClass('target');
        }
        
        //$(NDrag.Data[NDrag.TargetElement]).addClass('target');
    
//        var pos=$.getMousePosition(e);
//        var x=pos.x-this.ContainerPos.left;
//        var y=pos.y-this.ContainerPos.top;
//        var id=this._getTargetId(x,y);
//        if(id==null)return;
//        this.DragState=1;
//        if(typeof id=='string'){
//            if(id!=this.ActionName)$('#'+this.ActionName).removeClass('hover');
//            this._dragAction(id);
//            return false;
//        }
//        if(this.ActionName){
//            $('#'+this.ActionName).removeClass('hover');
//            this.ActionName=null;
//        }
//        id=id-1;
        //NDrag.TargetElement=id;
        if(NDrag.TargetElement==this.TempElement)return;
        this.dragOverBox(NDrag.TargetElement);
        this.TempElement=NDrag.TargetElement;
    },
    _getTargetId:function(x,y){
        var _x=x%90;
        var _y=y%90;
        var xn=Math.ceil(x/90);
        var yn=Math.ceil(y/90);
        if(yn>=3&&yn<=4&&xn>=4&&xn<=7){
        if(y<90*2+10||x>90*7)return null;
        if(this.State==2){
        if(x<=90*3+116)return'mine-edit';
        if(x<=90*3+116+118)return'mine-del';
            return'mine-copy';
        }else{
            return'common-copy';
        }
        }
        if(_x<10||_y>80)return null;
        if(yn<=2)return 10*(yn-1)+xn;
        if(yn>=5)return 32+10*(yn-5)+xn;
        if(xn<=3)return 20+3*(yn-3)+xn;
        if(xn>=8)return 26+3*(yn-3)+(xn-7);
        return null;
    }
    
};

